//
//  SearchSongViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SearchSongViewController.h"
#import "MusicPlyController.h"

#import "SonglistTableViewCell.h"
#import "SongListModel.h"
#import "UrlListModel.h"

@interface SearchSongViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , assign) NSInteger page;

@end

@implementation SearchSongViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    [self createTableView];
    self.songArr = [NSMutableArray array];
//    self.page = 1;
//    [self dataHandlerSearchSong];
}
- (void)setSearchText:(NSString *)searchText
{
    _searchText = searchText;
    
    self.page = 1;
    [self dataHandlerSearchSong:_searchText withNextPage:1];
    
    [self addFooter:_searchText];
    [self.tableView reloadData];
}

#pragma mark - 解析数据(歌曲)
- (void)dataHandlerSearchSong:(NSString *)searchText withNextPage:(NSInteger)currentPage{
    NSString *str = [NSString stringWithFormat:@"http://search.dongting.com/song/search?q=%@&page=%ld&size=50", searchText,currentPage];
    [NetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        if (self.page == 1) {
            self.songArr = [NSMutableArray array];
        }

        NSArray *arr = responseObject[@"data"];

        for (NSDictionary *smallDic in arr) {
            SongListModel *model = [[SongListModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.songArr addObject:model];
        }
        
        [_tableView reloadData];
    } filed:^(NSError *error) {

    }];
}

- (void)addFooter:(NSString *)searchText{
    
//    __block SearchSongViewController *root = self;
////    [self.tableView addLegendFooterWithRefreshingBlock:^{
//        root.page++;
//        [root dataHandlerSearchSong:searchText withNextPage:root.page];
//    }];

    
}

- (void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 80;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[SonglistTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.songArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SonglistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SongListModel *songModel = _songArr[indexPath.row];
    cell.numLabel.text = [@(indexPath.row + 1) stringValue];
    
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x9C9C9C);
    cell.songName.text = songModel.name;
    cell.singerName.text = songModel.singerName;
    if (self.searchText.length) {
        // 设置标签文字
        NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:songModel.name] ;
        // 获取标红的位置和长度
        NSRange range = [songModel.name rangeOfString:self.searchText options:NSCaseInsensitiveSearch];
        
        // 设置标签文字的属性
        [attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
        // 显示在Label上
        cell.songName.attributedText = attrituteString;
        
        // 设置标签文字
        NSMutableAttributedString *attrituteStringName = [[NSMutableAttributedString alloc] initWithString:songModel.singerName];
        // 获取标红的位置和长度
        NSRange rangeName = [songModel.singerName rangeOfString:self.searchText options:NSCaseInsensitiveSearch];
        
        // 设置标签文字的属性
        [attrituteStringName setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:rangeName];
        // 显示在Label上
        cell.singerName.attributedText = attrituteStringName;
    }
    
    
    NSInteger count = [songModel.favorites integerValue];
    NSString *str = nil;
    if (count >= 10000) {
        CGFloat num = count / 10000.0;
        if (count % 10000 >= 1000) {
            str = [NSString stringWithFormat:@"%.1f 万",num];
        } else {
            str = [NSString stringWithFormat:@"%.0f 万",num];
        }
    } else {
        str = [NSString stringWithFormat:@"%ld",count];
    }
    cell.favorites.text = str;
    return cell;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//   
//    
//    MusicPlyController *MusicPlay = [MusicPlyController musicplyVC];
//    
//    NSMutableArray *newArray = [NSMutableArray array];
//    
//    for (SongListModel *listModel in self.songArr) {
//        
//        UrlListModel *urlModel = [listModel.urlList firstObject];
//        
//        [newArray addObject:urlModel.url];
//    }
//    
//    
//    MusicPlay.MusicArray = [NSMutableArray arrayWithArray:newArray];
//    MusicPlay.index = indexPath.row;
//    
//    
//    [self presentViewController:MusicPlay animated:YES completion:^{
//        
//        
//    }];
//    
//
//    
//}


@end
